var wijzigLeerling = {};

wijzigLeerling.build = function() {

   Ti.Media.vibrate([0, 35]);
   
   var win = Ti.UI.createWindow({
       backgroundColor: '#fff',
       layout: 'vertical',
       navBarHidden:true,
   });

	var header = Ti.UI.createView({
	   width: Ti.UI.FILL,
	   height: '85dip',
	   backgroundImage: '/images/header.png',
	   top: 0
	});
	var title = Ti.UI.createLabel({
	    color: '#fff',
	    font: {fontSize:'20dip' },
	    top: '18dip',
	})
    title.text = 'Leerling wijzigen';
	
	header.add(title);
	win.add(header);
	
	if(Ti.Platform.name != 'android')
    {
        var backButton = Ti.UI.createButton({
            title: 'Terug', 
            left: '10dip',
            top: '10dip',
            width: Ti.UI.SIZE,
            height: Ti.UI.SIZE,
            color: '#090C16',    
            visible: true
        });
        
        backButton.addEventListener('click', function() {
            win.close;
        });
        
        header.add(backButton);
  	}
  	
	win.open();
	
	
	for(var i = 0; i < Globals.leerlingenArray.length; i++) {
   		if(Globals.leerlingenArray[i].id === leerlingen.leerlingRow.leerling.id) {
   			var leerling = Globals.leerlingenArray[i];
   		}
   	}
	var nieuweLeerlingPlaceholder = Ti.UI.createScrollView({
	   height: Ti.UI.FILL,
	   width: '300dip',
	   layout: 'vertical'
	});
	
	var naamTextBox = Ti.UI.createTextField({
	   hintText: 'Naam',
	   value: leerling.naam,
	   width: Ti.UI.FILL,
	   height: '60dip',
	   borderColor: '#000000',
	   borderWidth: '1',
	   backgroundColor: Globals.TFBackgroundColor
	});
	
	
	
	nieuweLeerlingPlaceholder.add(naamTextBox);
	
	var groepTextBox = Ti.UI.createTextField({
	   top: '10dip',
	   hintText: 'Groep',
	   value: leerling.klas,
	   width: Ti.UI.FILL,
	   height: '60dip',
	   borderColor: '#000000',
	   borderWidth: '1',
	   backgroundColor: Globals.TFBackgroundColor
	});
	
	nieuweLeerlingPlaceholder.add(groepTextBox);
	
	var email1TextBox = Ti.UI.createTextField({
	   top: '10dip',
	   hintText: 'E-mail adres 1',
	   value: leerling.mailadres,
	   width: Ti.UI.FILL,
	   height: '60dip',
	   borderColor: '#000000',
	   borderWidth: '1',
	   backgroundColor: Globals.TFBackgroundColor
	});
	
	var email2TextBox = Ti.UI.createTextField({
	   top: '10dip',
	   hintText: 'E-mail adres 2',
	   value: leerling.andermailadres,
	   width: Ti.UI.FILL,
	   height: '60dip',
	   borderColor: '#000000',
	   borderWidth: '1',
	   backgroundColor: Globals.TFBackgroundColor
	});
	
	nieuweLeerlingPlaceholder.add(email1TextBox);
	nieuweLeerlingPlaceholder.add(email2TextBox);
	
	
	var opslaanButton = Ti.UI.createButton({
		top: '10dip',
		backgroundImage: '/images/button_bg.png',
		right: 0,
		width: '150dip',
		height: '60dip',
		bottom: '10dip',
		color: '#fff',
		font: {fontSize:'18dip' },
		title: 'Opslaan'
	});
	
	opslaanButton.addEventListener('click', function(e){
	   
    	Ti.Media.vibrate([0, 35]);
    	
		naamTextBox.blur();
    	groepTextBox.blur();
    	email1TextBox.blur();
    	email2TextBox.blur();
    	
    	var checkname = leerlingen.checkName(naamTextBox);
    	//email validatie
		var checkmailone = leerlingen.emailChecker(email1TextBox);
		var checkmailtwo = leerlingen.emailChecker(email2TextBox);
		
		if(checkmailone && checkmailtwo && checkname) {
		    leerling.naam = naamTextBox.value;
		    leerling.klas = groepTextBox.value;
		    leerling.mailadres = email1TextBox.value;
		    leerling.andermailadres = email2TextBox.value;
		   
		    leerlingen.leerlingRow.title = leerling.naam;
		   	
			WijzigXML();
			
		   	win.close();
		}
		else {
			naamTextBox.addEventListener('change', leerlingen.checkNameEvent);
			email1TextBox.addEventListener('change', leerlingen.emailCheckerEvent);
			email2TextBox.addEventListener('change', leerlingen.emailCheckerEvent);
			
			errorstr = [];
			var msg = "";
			
			//check what's wrong
			if(!checkmailone && email1TextBox.value != "") {
				errorstr.push("'" + email1TextBox.hintText + "'");
			}
			if(!checkmailtwo && email2TextBox.value != "") {
				errorstr.push("'" + email2TextBox.hintText + "'");
			}
			if(naamTextBox.value == "") {
				msg += "'" + naamTextBox.hintText + "' is niet ingevuld! ";
				if(errorstr.length > 0) {
					msg += "\n";
				}
			}
			//Ultimate Error Message builder
			switch(errorstr.length) {
				case 0:
					Ti.UI.createAlertDialog({
					    message: msg,
					    ok: 'OK',
					    title: 'Foutmelding'
					}).show();
					break;
				case 1:
					Ti.UI.createAlertDialog({
					    message: msg + errorstr[0] + " is niet geldig!",
					    ok: 'OK',
					    title: 'Foutmelding'
					}).show();
					break;
				case 2:
					Ti.UI.createAlertDialog({
					    message: msg + errorstr[0] + " en " + errorstr[1] +" zijn niet geldig! \n",
					    ok: 'OK',
					    title: 'Foutmelding'
					}).show();
					break;
				default:
					for(var i = 0; i<errorstr.length - 2;i++) {
						msg += errorstr[i] + ", ";
					}
					Ti.UI.createAlertDialog({
						    message: msg + errorstr[(errorstr.length - 2)] + " en " + errorstr[(errorstr.length - 1)] +" zijn niet geldig!",
						    ok: 'OK',
						    title: 'Foutmelding'
					}).show();
					break;
			}
			
		}

	});
	
	nieuweLeerlingPlaceholder.add(opslaanButton);
	
	win.add(nieuweLeerlingPlaceholder);
	
	function WijzigXML() {
		var target = Globals.getSupportFile('leerlingen.xml');
		
		if(target.exists()) {
			var xmltext = target.read().text;
		    var doc = Ti.XML.parseString(xmltext);
		    
		    var nodelist = doc.getElementsByTagName("leerling");
		    
		    for(var i = 0; i<nodelist.length; i++ ) {
	    		if (nodelist.item(i).getAttributes().getNamedItem("leerlingid").nodeValue == leerling.id) {
		    		nodelist.item(i).getAttributes().getNamedItem("klas").setValue(escape(leerling.klas));
		    		nodelist.item(i).getAttributes().getNamedItem("naam").setValue(escape(leerling.naam));
		    		nodelist.item(i).getAttributes().getNamedItem("email").setValue(escape(leerling.mailadres + ";" + leerling.andermailadres));
	    		}
		    }
		    
		   	target.write(Ti.XML.serializeToString(doc));
	   }
	}

}